-- 月度活动   酒馆达标 悬赏大师

-- human.db.actMonthTask.drunk 

local Util = require("common.Util")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local MonthactExcel = require("excel.monthAct")
local MailExcel = require("excel.mail")
local Grid = require("bag.Grid")
local MonthActFill = require("monthAct.MonthDataFill")
local MonthActLogic = require("monthAct.MonthActLogic")
local MailManager = require("mail.MailManager")
local YunYingLogic = require("yunying.YunYingLogic")

MONTHACT_DRUNK_TYPE_1       = 1   --  酒馆达标活动  完成%d星 佣兵任务
MONTHACT_DRUNK_FINISH_TYPE  = 2   --  酒馆达标活动完成所有其他任务的任务type

local function getNowValue(human, id)
    if not human.db.actMonthTask or
        not human.db.actMonthTask.drunk then
        return 0
    end
    return human.db.actMonthTask.drunk[id] or 0
end

local function setNowValue(human, id, value)
    human.db.actMonthTask = human.db.actMonthTask or {}
    human.db.actMonthTask.drunk = human.db.actMonthTask.drunk or {}
    human.db.actMonthTask.drunk[id] = value
end

-- 检查酒馆活动是否全部完成
local function checkDrunkActAllFinish(human)
    local isok, finishId = isFinish(human)
    if not isok then return end

    local cf = MonthactExcel.drunkery[finishId]
    if not cf then return  end

    --检查是否已经触发过奖励
    if getNowValue(human, finishId) > 0 then
        return
    end

    -- 改db 发放奖励
    setNowValue(human, finishId, 1)
    ObjHuman.save(human)
     
    local title = MailExcel.mail[12].title
    local content = MailExcel.mail[12].content
    local senderName = MailExcel.mail[12].senderName
    MailManager.add(MailManager.SYSTEM, human.db._id, title, content, cf.reward, senderName)    
end

local function addDrunkCnt(human, id, addNum)
    --检查id是否非法
    local cf = MonthactExcel.drunkery[id]
    if not cf  then return end

    local oldStep = getNowValue(human, id)
    if oldStep >= cf.taskCnt then return end

    local newStep = math.min(oldStep + addNum, cf.taskCnt)
    setNowValue(human, id, newStep)
    if newStep < cf.taskCnt then return  end

    ObjHuman.save(human)
    --达成任务 发放奖励   并且检查是否全部完成 
    local title = MailExcel.mail[11].title
    local content = Util.format(MailExcel.mail[11].content, cf.special)
    local senderName = MailExcel.mail[11].senderName
    MailManager.add(MailManager.SYSTEM, human.db._id, title, content, cf.reward, senderName)
    checkDrunkActAllFinish(human)
    for k, v in pairs(funcID) do
        YunYingLogic.updateIcon(YYInfo[k], human)
        break
    end
end

function isFinish(human)
    for id, cf in ipairs(MonthactExcel.drunkery) do
        if cf.type == MONTHACT_DRUNK_FINISH_TYPE then
            return true, id
        end
        if getNowValue(human, id) < cf.taskCnt then
            return
        end
    end
end

--查询
function query(human)
    local msgRet = Msg.gc.GC_MONTHACT_DRUNK_QUERY
    msgRet.leftTime = MonthActLogic.getActLeftTime(human)
    for id, cf in ipairs(MonthactExcel.drunkery) do
        MonthActFill.makeMonthTaskData(msgRet.list[id], cf, id) 
        msgRet.list[id].cur = getNowValue(human, id) 
    end
    msgRet.list[0] = #MonthactExcel.drunkery 
    --Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

-- 酒馆达标活动计数入口        Ex 之前
function addDrunkCntEx(human, star, addNum)
    -- 检查活动是否结束
    MonthActLogic.checkActEndTime(human)

    star = star or 0
    for id, cf in ipairs(MonthactExcel.drunkery) do
        if cf.type == MONTHACT_DRUNK_TYPE_1 and star == cf.special then 
            addDrunkCnt(human, id, addNum)
            break 
        end
    end
end

--[[
function isRed(human)
    if not isFinish(human) then return true end
end
--]]

